package edu.example;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class SettingFrame extends JFrame
{
	//Vectors used for dropdown menu
	private Vector<String> reviewer = new Vector<String>();
	private Vector<Reviewer> reviewerInfo;
	private Vector<String> countries = new Vector<String>();
	private Vector<String> researchArea = new Vector<String>();
	
	//GUI Component
	private JComboBox reviewerComboBox;
	private JComboBox countryComboBox;
	private JComboBox countryComboBox1;
	private JComboBox researchAreaComboBox;
	private JComboBox researchAreaComboBox1;
	private JComboBox researchAreaComboBox2;
	
	private JLabel nameLabel;
	private JLabel nameLabel1;
	private JLabel emailLabel;
	private JLabel passwordLabel;
	private JLabel countryLabel;
	private JLabel researchAreaLabel;
	
	private JLabel workloadLabel;
	private JLabel workloadLabel1;
	private JLabel numOfAsgdReview;
	private JLabel numOfAsgdReview1;
	
	private JTextField emailField;
	private JTextField passwordField;
	
	private JButton editEmailButton;
	private JButton editPasswordButton;
	private JButton saveButton;
	
	private JPanel entryPanel;
	private JPanel savePanel;
	
	//Handlers
	private SelectHandler selectHandler;
	private EditHandler editHandler;
	private SaveHandler saveHandler;
	
	// data control
	private MainScreenFrame backScreen;
	private UserManager um;
	private Reviewer selected;
	/**
	 * Constructor
	 */
	public SettingFrame(MainScreenFrame initBackScreen, UserManager initUm)
	{
		backScreen = initBackScreen;
		um = initUm;
		intiData();
		layoutGUI();
		setSize(1080, 620);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	/**
	 * Initial data, ie., reviewer list, research area and country
	 */
	public void intiData()
	{
		try
		{
			FileInputStream fstream = new FileInputStream("CountryList.txt");
			  // Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			  //Read File Line By Line
			countries.add("");
			while ((strLine = br.readLine()) != null)   
			{
				countries.add(strLine);
			}
				in.close();
		}
		catch (Exception e)
		{
			System.err.println("Error: " + e.getMessage());
		}
		
		researchArea.add("");
		researchArea.add("Artificial Intelligence");
		researchArea.add("Graphics and Visualization");
		researchArea.add("Networks");
		researchArea.add("Programming languages/Verification");
		researchArea.add("Security");
		researchArea.add("Systems");
		researchArea.add("Theory");
		researchArea.add("Vision/Natural Language Processing");
		
		reviewerInfo = um.getReviewerList();
		reviewer.add("");
		for (int i=0; i<reviewerInfo.size(); i++)
			reviewer.add(reviewerInfo.get(i).getName());
	}
	
	public void layoutGUI()
	{
		//Construct GUI components
		this.setTitle("Settings");
		reviewerComboBox = new JComboBox(reviewer);
		reviewerComboBox.setBackground(Color.WHITE);
		countryComboBox = new JComboBox(countries);
		countryComboBox.setEnabled(false);
		countryComboBox.setBackground(Color.WHITE);
		countryComboBox1 = new JComboBox(countries);
		countryComboBox1.setEnabled(false);
		countryComboBox1.setBackground(Color.WHITE);
		researchAreaComboBox = new JComboBox(researchArea);
		researchAreaComboBox.setEnabled(false);
		researchAreaComboBox.setBackground(Color.WHITE);
		researchAreaComboBox1 = new JComboBox(researchArea);
		researchAreaComboBox1.setEnabled(false);
		researchAreaComboBox1.setBackground(Color.WHITE);
		researchAreaComboBox2 = new JComboBox(researchArea);
		researchAreaComboBox2.setEnabled(false);
		researchAreaComboBox2.setBackground(Color.WHITE);
		
		nameLabel= new JLabel("Name: ");
		nameLabel1 = new JLabel();
		emailLabel = new JLabel("E-Mail: ");
		passwordLabel = new JLabel("Password: ");
		countryLabel = new JLabel("Country: ");
		researchAreaLabel = new JLabel("Research Area: ");
		
		emailField = new JTextField(10);
		emailField.setEditable(false);
		editEmailButton = new JButton("Edit");
		editEmailButton.setEnabled(false);
		
		passwordField = new JTextField(10);
		passwordField.setEditable(false);
		editPasswordButton = new JButton("Edit");
		editPasswordButton.setEnabled(false);
		
		workloadLabel = new JLabel("Workload: ");
		workloadLabel1 = new JLabel();
		numOfAsgdReview = new JLabel("Number of assigned review: ");
		numOfAsgdReview1 = new JLabel();
		
		saveButton = new JButton("Save");
		saveButton.setEnabled(false);
		
		entryPanel = new JPanel();
		savePanel = new JPanel();
		entryPanel.setBackground(Color.WHITE);
		savePanel.setBackground(Color.WHITE);
		
		savePanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		savePanel.add(saveButton);
		
		//Setup layout
		GridBagLayout gbl = new GridBagLayout();
		entryPanel.setLayout(gbl);
		
		addJComponentToContainerUsingGBL(reviewerComboBox, 				entryPanel,	0,	0,	1,	1);
		addJComponentToContainerUsingGBL(nameLabel, 					entryPanel,	0,	1,	1,	1);
		addJComponentToContainerUsingGBL(nameLabel1, 					entryPanel,	1,	1,	1,	1);
		addJComponentToContainerUsingGBL(emailLabel, 					entryPanel,	0,	2,	1,	1);
		addJComponentToContainerUsingGBL(emailField, 					entryPanel,	1,	2,	1,	1);
		addJComponentToContainerUsingGBL(editEmailButton, 				entryPanel,	2,	2,	1,	1);
		addJComponentToContainerUsingGBL(passwordLabel, 				entryPanel,	0,	3,	1,	1);
		addJComponentToContainerUsingGBL(passwordField, 				entryPanel,	1,	3,	1,	1);
		addJComponentToContainerUsingGBL(editPasswordButton, 			entryPanel,	2,	3,	1,	1);
		addJComponentToContainerUsingGBL(countryLabel, 					entryPanel,	0,	4,	1,	1);
		addJComponentToContainerUsingGBL(countryComboBox, 				entryPanel,	1,	4,	1,	1);
		addJComponentToContainerUsingGBL(countryComboBox1, 				entryPanel,	2,	4,	1,	1);
		addJComponentToContainerUsingGBL(researchAreaLabel, 			entryPanel,	0,	5,	1,	1);
		addJComponentToContainerUsingGBL(researchAreaComboBox, 			entryPanel,	1,	5,	1,	1);
		addJComponentToContainerUsingGBL(researchAreaComboBox1,			entryPanel,	2,	5,	1,	1);
		addJComponentToContainerUsingGBL(researchAreaComboBox2,			entryPanel,	3,	5,	1,	1);
		addJComponentToContainerUsingGBL(workloadLabel,					entryPanel,	0,	6,	1,	1);
		addJComponentToContainerUsingGBL(workloadLabel1,				entryPanel,	1,	6,	1,	1);
		addJComponentToContainerUsingGBL(numOfAsgdReview,				entryPanel,	0,	7,	1,	1);
		addJComponentToContainerUsingGBL(numOfAsgdReview1,				entryPanel,	1,	7,	1,	1);
		
		this.add(savePanel, BorderLayout.SOUTH);
		this.add(entryPanel, BorderLayout.CENTER);
		
		//Construct handler
		selectHandler = new SelectHandler();
		editHandler = new EditHandler();
		saveHandler = new SaveHandler();
		
		//Link component to hander
		reviewerComboBox.addActionListener(selectHandler);
		saveButton.addActionListener(saveHandler);
		editEmailButton.addActionListener(editHandler);
		editPasswordButton.addActionListener(editHandler);
		
		// size and locate the dialog at the center of the screen
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - 1080)/2;
		int y = (screenHeight - 620)/2;
		setLocation(x,y);
		
	}
	
	/**
	 * GridBadLayout helper method
	 */
	public void addJComponentToContainerUsingGBL(	JComponent jc,
			Container c,
			int x,
			int y,
			int w,
			int h)
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}
	
	class SelectHandler implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			if (((JComboBox) arg0.getSource()).getSelectedItem().equals("")){
				countryComboBox.setEnabled(false);
				countryComboBox1.setEnabled(false);
				researchAreaComboBox.setEnabled(false);
				researchAreaComboBox1.setEnabled(false);
				researchAreaComboBox2.setEnabled(false);
				editEmailButton.setEnabled(false);
				editPasswordButton.setEnabled(false);
				saveButton.setEnabled(false);
				
				countryComboBox.setSelectedItem("");
				countryComboBox1.setSelectedItem("");
				researchAreaComboBox.setSelectedItem("");
				researchAreaComboBox1.setSelectedItem("");
				researchAreaComboBox2.setSelectedItem("");
				
				nameLabel1.setText("");
				emailField.setText("");
				passwordField.setText("");
				workloadLabel1.setText("");
				numOfAsgdReview1.setText("");
				
				selected = null;
			}
			else {
				countryComboBox.setEnabled(true);
				countryComboBox1.setEnabled(true);
				researchAreaComboBox.setEnabled(true);
				researchAreaComboBox1.setEnabled(true);
				researchAreaComboBox2.setEnabled(true);
				editEmailButton.setEnabled(true);
				editPasswordButton.setEnabled(true);
				saveButton.setEnabled(true);
				
				int index = ((JComboBox) arg0.getSource()).getSelectedIndex()-1;
				selected = reviewerInfo.get(index);
				countryComboBox.setSelectedItem(selected.getHomeCountry1());
				countryComboBox1.setSelectedItem(selected.getHomeCountry2());
				
				researchAreaComboBox.setSelectedItem(selected.getResearchArea1());
				researchAreaComboBox1.setSelectedItem(selected.getResearchArea2());
				researchAreaComboBox2.setSelectedItem(selected.getResearchArea3());
				
				nameLabel1.setText(selected.getName());
				emailField.setText(selected.getEMailAddress());
				passwordField.setText(selected.getPassword());
				workloadLabel1.setText(""+selected.getWorkload());
				numOfAsgdReview1.setText(""+selected.getNumAssignedReviews());
				
			}
			
		}
		
	}
	
	class EditHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			//Determine which field need to be changed
			if(ae.getSource() == editEmailButton)
			{
				emailField.setEditable(true);
			}
			if(ae.getSource() == editPasswordButton)
			{
				passwordField.setEditable(true);
			}
		}
	}
	
	class SaveHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			selected.setEMailAddress(emailField.getText());
			selected.setPassword(passwordField.getText());
			Vector<String> country = new Vector<String>();
			country.add((String) countryComboBox.getSelectedItem());
			country.add((String) countryComboBox1.getSelectedItem());
			selected.setHomeCountry1((String) countryComboBox.getSelectedItem());
			selected.setHomeCountry2((String) countryComboBox1.getSelectedItem());
			Vector<String> area = new Vector<String>();
			area.add((String) researchAreaComboBox.getSelectedItem());
			area.add((String) researchAreaComboBox1.getSelectedItem());
			area.add((String) researchAreaComboBox2.getSelectedItem());
			selected.setResearchArea1((String) researchAreaComboBox.getSelectedItem());
			selected.setResearchArea2((String) researchAreaComboBox1.getSelectedItem());
			selected.setResearchArea3((String) researchAreaComboBox2.getSelectedItem());
			um.updateUser(selected);
			
			System.out.println(selected.toString());
			
			SettingFrame.this.setVisible(false);
			MainScreenFrame frame = new MainScreenFrame(um);
			frame.setVisible(true);
		}
	}
	
	public static void main(String[] args)
	{
		UserManager u = new UserManager();
		u.login("chair", "abc");
		MainScreenFrame m = new MainScreenFrame(u);
		SettingFrame frame = new SettingFrame(m,u);
		frame.setVisible(true);
	}
}
